package com.entersoft.sicep.log4j;

import org.apache.log4j.Logger;

/**
 * Package: log4j
 * Clase: Log4jUtil
 * Descripción: LOG del aplicativo
 * Versión SICEP: 3.0
 * Autor: Cesar Pino - Entersoft
 * fecha creación: xx/xx/2012
 * ----------------------------------
 * 
 * Modificado por: XXXXXXXXXXXXXXXXXX
 * Motivo de actualización: XXXXXXXXX
 * XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 * fecha actualización: xx/xx/2012
 */
public final class Log4jUtil {


    private static final Logger logger = Logger.getLogger(Log4jUtil.class);
    private static Log4jUtil s_instance;
    private static final String GUION = " - ";

    static{
            try {
                    UtilLog4J.getInstance();
            }
            catch (Exception e) {
                    if(logger != null){
                            logger.error("Error en Logger (static), Class -> ["+ e.getClass().getName() +"] Message -> ["+ e.getMessage() +"]", e);
                    }
            }
    }

    private Log4jUtil(){		super();}

    public static Log4jUtil instance() {
            synchronized(Log4jUtil.class){
                    if (s_instance == null) {
                            s_instance = new Log4jUtil();
                    }
            }
            return s_instance;
    }

    private Logger getLogger(){
              return logger;
    }

    public void debug(Class className, Object object)
      {
            if (object == null)
                    return;
            else
                    getLogger().debug(className.getName() + GUION + object);

      }

    public void error(Class className, Object object, Throwable t)
      {

                    getLogger().error(className.getName() + GUION + object, t);

      }

    public void info(Class className, Object object) {
            if (object == null)
                    return;
            else
                    getLogger().info(className.getName() + GUION + object);

    }

    public org.apache.log4j.Category getCategory(Class p_class) {
                    org.apache.log4j.Category result = org.apache.log4j.LogManager.getRootLogger();

            try {
                    if (p_class != null) {
                            String cName = p_class.getName();
                            String categoryName = cName;
                            result = org.apache.log4j.LogManager.getLogger(categoryName);
                    }
            } catch (Exception e) {
                    this.error(this.getClass(), e, e);
            }

            return result;
    }

    /**
     * @param class1
     * @param string
     */
    public void error(Class className, String string) {
            getLogger().error(className.getName() + GUION + string);
    }
}
